/*
 * StartApplication.java
 *
 * Created on Apr 11, 2008 8:41:18 AM;
 */

package org.sunspotworld;

import com.sun.spot.peripheral.Spot;
import com.sun.spot.sensorboard.EDemoBoard;
import com.sun.spot.sensorboard.peripheral.ISwitch;
import com.sun.spot.sensorboard.peripheral.ITriColorLED;
import com.sun.spot.peripheral.radio.IRadioPolicyManager;
import com.sun.spot.io.j2me.radiostream.*;
import com.sun.spot.io.j2me.radiogram.*;
import com.sun.spot.util.*;

import com.sun.spot.sensorboard.io.IIOPin;
import com.sun.spot.sensorboard.io.OutputPin;
import com.sun.spot.sensorboard.peripheral.Servo;

import java.io.*;
import javax.microedition.io.*;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;

/**
 * The startApp method of this class is called by the VM to start the
 * application.
 * 
 * The manifest specifies this class as MIDlet-1, which means it will
 * be selected for execution.
 */
public class StartApplication extends MIDlet {
    
    //private ITriColorLED [] leds = EDemoBoard.getInstance().getLEDs();
    EDemoBoard demo = EDemoBoard.getInstance();
    Servo sonarServo = new Servo(demo.getIOPins()[EDemoBoard.D2]);
//    IIOPin motorControl = demo.getIOPins()[demo.D3];
    ISwitch sw1                  = demo.getSwitches()[0];  
    //ISwitch sw2                  = demo.getSwitches()[1];  
    //long lastPing;
    //int pingDelay = 25;  //delay between pings in milliseconds
    //IIOPin pinD2 = demo.getIOPins()[EDemoBoard.D2];
/*
    protected void run() throws IOException {
        while(sw1.isOpen()){
            
        }
    }
  */  
    protected void startApp() throws MIDletStateChangeException {
        System.out.println("Hello, world");
        new BootloaderListener().start();   // monitor the USB (if connected) and recognize commands from host
        new SpotBot();
        
        notifyDestroyed();                      // cause the MIDlet to exit
    }

    protected void pauseApp() {
        // This is not currently called by the Squawk VM
    }

    /**
     * Called if the MIDlet is terminated by the system.
     * I.e. if startApp throws any exception other than MIDletStateChangeException,
     * if the isolate running the MIDlet is killed with Isolate.exit(), or
     * if VM.stopVM() is called.
     * 
     * It is not called if MIDlet.notifyDestroyed() was called.
     *
     * @param unconditional If true when this method is called, the MIDlet must
     *    cleanup and release all resources. If false the MIDlet may throw
     *    MIDletStateChangeException  to indicate it does not want to be destroyed
     *    at this time.
     */
    protected void destroyApp(boolean unconditional) throws MIDletStateChangeException {
        //for (int i = 0; i < 8; i++) {
        //    leds[i].setOff();
        //}
    }
}
